package tcm.com.gistone.controller;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import tcm.com.gistone.database.config.GetBySqlMapper;
import tcm.com.gistone.database.entity.Bookinfo;
import tcm.com.gistone.database.entity.Special;
import tcm.com.gistone.database.mapper.BookinfoMapper;
import tcm.com.gistone.database.mapper.SpecialMapper;
import tcm.com.gistone.util.ClientUtil;
import tcm.com.gistone.util.ConfigUtil;
import tcm.com.gistone.util.EdatResult;
import tcm.com.gistone.util.FileUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:renqiang
 * @Param:
 * @Description:专题
 * @Date:14:49 2017/7/9
 */
@RestController
@RequestMapping("special")
@Transactional
public class SpecialController {
    @Autowired
    private SpecialMapper sm;
    @Autowired
    BookinfoMapper bm;
    @Autowired
    private GetBySqlMapper gm;
    @Autowired
    private ConfigUtil configUtil;


    /*查询专题下的古籍*/
    @RequestMapping(value = "getBookBySpecial", method = RequestMethod.POST)
    public EdatResult getBookBySpecial(HttpServletRequest request,
                                       HttpServletResponse response) {
        try {
            ClientUtil.SetCharsetAndHeader(request, response);
            JSONObject data = JSONObject.fromObject(request
                    .getParameter("data"));
            long specialId = data.getLong("specialId");
            String sql = "select book_id from tb_special_book where special_id = " + specialId;
            List<Map> result = new ArrayList<>();
            List<Bookinfo> res = new ArrayList<>();
            result = gm.findRecords(sql);
            for (int i = 0; i < result.size(); i++) {
                Bookinfo bookinfo = new Bookinfo();
                bookinfo = bm.selectByPrimaryKey((long) result.get(i).get("book_id"));
                res.add(bookinfo);
            }
            return EdatResult.build(0, "success", res);
        } catch (Exception e) {
            e.printStackTrace();
            return EdatResult.build(1, "查询失败");
        }
    }

    /*查询所有专题*/
    @RequestMapping("getAllSpecial")
    public EdatResult getAllSpecial(HttpServletRequest request,
                                    HttpServletResponse response) {
        try {
            ClientUtil.SetCharsetAndHeader(request, response);
            List<Special> specials = sm.selectAll();
            return EdatResult.build(0, "success", specials);
        } catch (Exception e) {
            e.printStackTrace();
            return EdatResult.build(1, "查询失败");
        }
    }

    /*获取日志信息*/
    @RequestMapping(value = "getLog")
    public EdatResult getLog(HttpServletRequest request,
                             HttpServletResponse response) {
        try {
            ClientUtil.SetCharsetAndHeader(request, response);
            String log = FileUtil.readTxtFile(configUtil.getLogUrl());
            Map map = new HashMap();
            map.put("log", log);
            return EdatResult.build(0, "success", map);
        } catch (Exception e) {
            e.printStackTrace();
            return EdatResult.build(1, "查询失败");
        }
    }

}
